<template>
    <label class="bc-checkbox">
        <span class="bc-checkbox__input" :class="{'is-checked':isChecked,
            'is-disabled':disabled, 'is-indeterminate':isIndeterminate}">
            <input class="bc-checkbox__original" type="checkbox" v-model="checked"
             :disabled="disabled" :value="value" @change="handleChange">
            <span class="bc-checkbox__inner"></span>
        </span>
        <span class="bc-checkbox__label" v-if="label" v-text="label"></span>
        <span class="bc-checkbox-text" v-else><slot></slot></span>
    </label>
</template>

<script>
export default {
    name: 'BcCheckbox',
    props: {
        label: String,
        disabled: Boolean,
        value: {},
        modelValue: {},
        indeterminate: Boolean
    },
    model: {
        prop: 'modelValue',
        event: 'change'
    },
    data() {
        return {
            checked: this.modelValue
        }
    },
    watch: {
        modelValue(val) {
            this.checked = val
        }
    },
    computed: {
        // 1. 不直接在data里checked: this.modelValue，是因为此种方式只能在初始化时给checked绑定上值,
        // 后续父组件改变props里modelValue的值checked不会更新。
        // 2. 在勾选选择框时发现checked的值有变化，这是因为checked跟input的checked(v-model)属性绑定了，
        // 是子组件内部导致的值的变化，而非子组件$emit给父组件改变props里的值导致的checked值的变化。
        // 3. 不能把checked放在计算属性里，因为计算属性不能直接通过v-model直接改值，不然会报如下错误：
        // Computed property "checked" was assigned to but it has no setter.
        // checked() {
        //     return this.modelValue
        // },
        isChecked() {
            // 不能直接return this.checked; js里非零非false,null,undefined 都会转换为true
            // 若父组件中modelValue传进来的是[3]，给组件设置value值为2，这样isChecked为true，而实际是没勾选。
            if ({}.toString.call(this.checked) === '[object Boolean]') {
                return this.checked;
            } else if (Array.isArray(this.checked)) {
                // return this.checked.indexOf(this.value) > -1;
                return this.checked.includes(this.value)
            } else {
                return false
            }
        },
        isIndeterminate() {
            if(this.isChecked) {
                return false
            } else {
                return this.indeterminate
            }
        }
    },
    methods: {
        handleChange() {
            console.log("fff")
            this.$emit('change', this.checked)
        }
    }
}
</script>

<style scoped>
.bc-checkbox {
    color: #606266;
    font-weight: 500;
    font-size: 14px;
    position: relative;
    cursor: pointer;
    display: inline-block;
    white-space: nowrap;
    user-select: none;
    margin-right: 30px;
}
.bc-checkbox:last-of-type {
    margin-right: 0;
}
.bc-checkbox__original {
    opacity: 0;
    outline: none;
    position: absolute;
    margin: 0;
    width: 0;
    height: 0;
    z-index: -1;
}
.bc-checkbox__input {
    white-space: nowrap;
    cursor: pointer;
    outline: none;
    display: inline-block;
    line-height: 1;
    position: relative;
    vertical-align: middle;
}
.bc-checkbox__input.is-checked.is-disabled .bc-checkbox__inner {
    background-color: #f2f6fc;
    border-color: #dcdfe6;
}
.bc-checkbox__input.is-disabled .bc-checkbox__inner {
    cursor: not-allowed;
    background-color: #f2f6fc;
    border-color: #dcdfe6;
}
.bc-checkbox__input.is-checked .bc-checkbox__inner {
    background-color: #409eff;
    border-color: #409eff;
}
.bc-checkbox__inner {
    display: inline-block;
    position: relative;
    border: 1px solid #dcdfe6;
    border-radius: 2px;
    box-sizing: border-box;
    width: 16px;
    height: 16px;
    background-color: #fff;
    z-index: 1;
    transition: border-color .25s cubic-bezier(.71,-.46,.29,1.46),background-color .25s cubic-bezier(.71,-.46,.29,1.46);
}
.bc-checkbox__input.is-checked .bc-checkbox__inner:after {
    transform: rotate(45deg) scaleY(1);
}
.bc-checkbox__input.is-checked .bc-checkbox__inner:after {
    content: "";
    display: table;
    width: 4px;
    height: 8px;
    position: absolute;
    top: 1px;
    left: 4px;
    border: 2px solid #fff;
    border-top: 0;
    border-left: 0;
    transform: rotate(45deg) scale(1);
    transition: all .2s ease-in-out;
}
.bc-checkbox__input.is-disabled.is-checked .bc-checkbox__inner:after {
    border-color: #c0c4cc;
}
.bc-checkbox__input.is-indeterminate .bc-checkbox__inner {
    background-color: #409eff;
    border-color: #409eff;
}
.bc-checkbox__input.is-indeterminate.is-disabled .bc-checkbox__inner {
    background-color: #f2f6fc;
    border-color: #dcdfe6;
}
.bc-checkbox__input.is-indeterminate .bc-checkbox__inner:before {
    content: "";
    position: absolute;
    display: block;
    background-color: #fff;
    width: 10px;
    height: 2px;
    left: 2px;
    right: 0;
    top: 6px;
}
.bc-checkbox__input.is-indeterminate.is-disabled .bc-checkbox__inner:before {
    background-color: #c0c4cc;
}
.bc-checkbox__input.is-indeterminate .bc-checkbox__inner::after {
    display: none;
}
</style>